package com.rms.hkpc.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.rms.hkpc.R;
import com.rms.hkpc.activity.RecordsDetailActivity;
import com.rms.hkpc.adapter.RecordsAdapter;
import com.rms.hkpc.adapter.base.BaseAdapter;
import com.rms.hkpc.apiresult.ApiResultData;
import com.rms.hkpc.apiresult.PagedList;
import com.rms.hkpc.db.bean.Event;
import com.rms.hkpc.db.dto.WorkOrderRecordDto;
import com.rms.hkpc.db.dto.WorksOrderDto;
import com.rms.hkpc.db.query.BaseQuery;
import com.rms.hkpc.constant.AllConstants;
import com.rms.hkpc.control.LinearLayoutManager;
import com.rms.hkpc.db.query.BaseRecordQuery;
import com.rms.hkpc.fragment.base.BaseListFragment;
import com.rms.hkpc.presenter.RecordsConPresenter;
import com.rms.hkpc.util.NetworkUtil;
import com.rms.hkpc.view.IRecordsConView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class RecordsConFragment extends BaseListFragment<IRecordsConView, RecordsConPresenter, WorkOrderRecordDto, BaseRecordQuery> implements IRecordsConView {

    private RecordsAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_records_con;
    }

    @Override
    protected RecordsConPresenter createPresenter() {
        return new RecordsConPresenter();
    }

    @Override
    protected BaseRecordQuery createQuery() {
        return new BaseRecordQuery();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        adapter = new RecordsAdapter(context, mData, R.layout.rvitem_records);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                if (mData.isEmpty()) {
                    Toast.makeText(context,R.string.refresh_data,Toast.LENGTH_LONG).show();
                    return;
                }
                Intent intent = new Intent(context, RecordsDetailActivity.class);
                intent.putExtra(AllConstants.ID,"/api/esp/workOrderRecords?id="+mData.get(position).getId());
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        mPresenter.getData(query);
    }

    @Override
    protected void search() {
        query.init();
        query.filter();
        mPresenter.getData(query);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == 0) {
            query.init();
            query.filter();
            if (!TextUtils.isEmpty(event.getBat())) {
                query.setBat(event.getBat());
                query.setEat(event.getEat());
            }
            query.setCode(event.getCode());
            if (!TextUtils.isEmpty(event.getItem())) {
                query.setName(event.getItem());
            }
            if (!TextUtils.isEmpty(event.getValue())) {
                query.setVal(Integer.parseInt(event.getValue()));
            }
            if (event.getResult() != 2) {
                query.setResult(event.getResult());
            }
            mData.clear();
            mPresenter.getData(query);
        }else if (event.getWhat() == 300){
            mData.clear();
            search();
        }
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<WorkOrderRecordDto>> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() != 2000) {
            Toast.makeText(context, apiResult.getMessage(), Toast.LENGTH_LONG).show();
            adapter.notifyDataSetChanged();
            return;
        }
        mData.addAll(apiResult.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFails(Exception e) {
        refreshSmartRefreshLayout();
        adapter.notifyDataSetChanged();
        Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
    }
}
